library(SingleCellExperiment)

counts_matrix <- data.frame(
  cell_1 = rpois(10, 10),
  cell_2 = rpois(10, 10),
  cell_3 = rpois(10, 30)
)
rownames(counts_matrix) <- paste0("gene_", 1:10)
counts_matrix <- as.matrix(counts_matrix) # must be a matrix object!

sce <- SingleCellExperiment(assays = list(counts = counts_matrix))
sce <- scater::logNormCounts(sce)
assays(sce)

counts_100 <- counts(sce) + 100
assay(sce, "counts_100") <- counts_100 # assign a new entry to assays slot
assays(sce) # new assay has now been added.


cell_metadata <- data.frame(batch = c(1, 1, 2))
rownames(cell_metadata) <- paste0("cell_", 1:3)

sce <- SingleCellExperiment(
  assays = list(counts = counts_matrix),
  colData = cell_metadata
)
sce <- scater::addPerCellQC(sce)
colData(sce)


my_genes <- c("gene_1", "gene_5")
metadata(sce) <- list(favorite_genes = my_genes)
metadata(sce)
your_genes <- c("gene_4", "gene_8")
metadata(sce)$your_genes <- your_genes
metadata(sce)


sce <- scater::logNormCounts(sce)
sce <- scater::runPCA(sce)
reducedDim(sce, "PCA")

sce <- scater::runTSNE(sce, perplexity = 0.1)
reducedDim(sce, "TSNE")
reducedDims(sce)

u <- uwot::umap(t(logcounts(sce)), n_neighbors = 2)
reducedDim(sce, "UMAP_uwot") <- u
reducedDims(sce) # Now stored in the object.
reducedDim(sce, "UMAP_uwot")

sce <- scater::runUMAP(sce, n_neighbors = 2)
reducedDim(sce, "UMAP")
reducedDims(sce)


spike_counts <- cbind(cell_1 = rpois(5, 10), 
    cell_2 = rpois(5, 10), 
    cell_3 = rpois(5, 30))
rownames(spike_counts) <- paste0("spike_", 1:5)
spike_se <- SummarizedExperiment(list(counts=spike_counts))
spike_se

altExp(sce, "spike") <- spike_se
altExps(sce)


sce <- scran::computeSumFactors(sce)
sizeFactors(sce)

sizeFactors(sce) <- scater::librarySizeFactors(sce)
sizeFactors(sce)

colLabels(sce) <- LETTERS[1:3]
colLabels(sce)
